package com.tbit.uqbike.service.business.impl;


import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AlarmMsg;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.AlarmMsgService;
import com.tbit.uqbike.webmanager.dao.core.AlarmMsgDao;
import com.tbit.uqbike.webmanager.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("alarmMsgService")
public class AlarmMsgServiceImpl implements AlarmMsgService {
    @Autowired
    private AlarmMsgDao alarmMsgDao;
    @Autowired
    private AccountService accountService;
    @Autowired
    private MachineService machineService;

    @Override
    public Integer queryCount(LoginInfo loginInfo, String userCode, Integer alarmType) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Integer> accountIds;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            accountIds = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountIds = accountService.getByBrandId(loginInfo.id);
        } else if (loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
            List<Account> account = accountService.getOperationAccountIds(loginInfo.id, loginInfo);
            accountIds = new ArrayList<>();
            if (account.size() > 0) {
                for (Account a : account) {
                    accountIds.add(a.getAccountId());
                }
            }
        } else {
            accountIds = new ArrayList<>();
            accountIds.add(loginInfo.id);
        }
        if (userCode != null) {
            Machine machine = machineService.getByUserCode(userCode);
            if (!accountIds.contains(machine.getAccountId())) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
            params.put("machineId", machine.getMachineId());
        }
        params.put("accountIds", accountIds);
        params.put("alarmType", alarmType);

        return alarmMsgDao.queryCount(params);
    }

    @Override
    public List<AlarmMsg> queryPage(LoginInfo loginInfo, String userCode, Integer alarmType, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Integer> accountIds;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            accountIds = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountIds = accountService.getByBrandId(loginInfo.id);
        } else if (loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
            List<Account> account = accountService.getOperationAccountIds(loginInfo.id, loginInfo);
            accountIds = new ArrayList<>();
            if (account.size() > 0) {
                for (Account a : account) {
                    accountIds.add(a.getAccountId());
                }
            }
        } else {
            accountIds = new ArrayList<>();
            accountIds.add(loginInfo.id);
        }
        if (userCode != null) {
            Machine machine = machineService.getByUserCode(userCode);
            if (!accountIds.contains(machine.getAccountId())) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
            params.put("machineId", machine.getMachineId());
        }
        params.put("alarmType", alarmType);
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);

        return alarmMsgDao.queryPage(params);
    }

    @Override
    public void delBatch(List<String> msgIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("msgIds", StringUtil.getArrayListString(msgIds));

        alarmMsgDao.delBatch(params);
    }

    @Override
    public void delAll(Integer accountId) {
        alarmMsgDao.delAll(accountId);
    }

    @Override
    public void add(AlarmMsg alarmMsg) {
        alarmMsgDao.insert(alarmMsg);
    }

    @Override
    public void addBatch(List<AlarmMsg> alarmMsgs) {
        alarmMsgDao.addBatch(alarmMsgs);
    }

    @Override
    public void delList(List<Integer> accountId) {
        alarmMsgDao.delList(accountId);
    }

    @Override
    public void clearInvalid(Integer days) {
        alarmMsgDao.clearInvalid(days);
    }

}  